package by.averianov.discounta.webapp.page.user.addcard;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.datamodel.enums.CardStatus;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.app.BasicAuthenticationSession;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.homepage.HomePage;

public class AddCard extends BaseLayout {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AddCard.class);
	
	private PageCreator pageCreator;
	private UserProfile sessionProfile;
	
	public AddCard(PageCreator pageCreator) {
		super(null);
		this.pageCreator = pageCreator;	
	}

	@Inject
	private UserService userService;

	@Override
	protected void onInitialize() {
		super.onInitialize();
		if(BasicAuthenticationSession.get().isSignedIn()) {
			this.sessionProfile = BasicAuthenticationSession.get().getUserProfile();
		} else {
			HomePage page = new HomePage();
			page.error("User must be Logged");
			setResponsePage(page);			
		}
		LOGGER.debug("Session User From AddCard: " + sessionProfile);
		
		final TextField<String> cardNumberText = new TextField<String>("cardNumber", Model.of(""));
		final TextField<String> activateCodeText = new TextField<String>("activateCode", Model.of(""));
		
		
		Form<?> form = new Form<Void>("cardAdd") {
			@Override
			protected void onSubmit() {

				Card resultingCard = null;
				Page page = pageCreator.createPage();
				try {
					resultingCard = userService.getCardById(Long.valueOf(cardNumberText.getModelObject().toString()));
					LOGGER.debug("Current Card: " + resultingCard);
					try {
						String resultingActivateCode = activateCodeText.getModelObject().toString();
						if (resultingCard.getActivateCode().equals(resultingActivateCode)) {
				        	resultingCard.setUserProfile(sessionProfile);
				        	resultingCard.setCardStatus(CardStatus.cardstatus_active);
				        	resultingCard.setCardDateStart(userService.getCurrentDate());
				        	resultingCard.setUserEditor(sessionProfile);
				        	page.info(new ResourceModel("p.login.addcard.cardadded").getObject());
				        	userService.saveOrUpdateCard(resultingCard);
				        }
					} catch (Exception e) {
						page = new AddCard(pageCreator);
						page.error(new ResourceModel("p.login.addcard.badactivatecode").getObject());
					}
				} catch (Exception e) {
					page = new AddCard(pageCreator);
					page.error(new ResourceModel("p.login.addcard.nocard").getObject());
				}
				LOGGER.debug("Current Card: " + resultingCard);
				
				setResponsePage(page);
			}
		};		 
		add(form);

		form.add(cardNumberText);		
		form.add(activateCodeText);
				
		Link linkCancel = new Link("linkCancel"){
			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
				
			}
		};
		linkCancel.add(new Label("labelCancel", getButtonCancel()));
		form.add(linkCancel);
	}

}
